package com.yunpuvip.sachet.modular.util;

import cn.hutool.core.util.ArrayUtil;
import com.yunpuvip.sachet.modular.core.ThreadContextValue;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * rest log 封装的类
 *
 * @author hyf
 * @date 2019-03-13
 **/
@Slf4j
public class LogUtil {

    public static void info(Logger logger, String format, Object... arguments) {
        StringBuilder stringBuilder = new StringBuilder("reqId:{},");
        stringBuilder.append(format);

        logger.info(stringBuilder.toString(), wrapDate(arguments));

    }

    public static void error(Logger logger, String format, Object... arguments) {
        StringBuilder stringBuilder = new StringBuilder("reqId:{},");
        stringBuilder.append(format);

        logger.error(stringBuilder.toString(), wrapDate(arguments));

    }


    private static Object[] wrapDate(Object... arguments) {
        Object[] newObjects = new Object[arguments.length + 1];
        ArrayUtil.copy(arguments, 0, newObjects, 1, arguments.length);
        newObjects[0] = ThreadContextValue.THREAD_UID.get();
        return newObjects;
    }


    public static void main(String[] args) {
        LogUtil.error(log, "hello {},{},{}", 1, 2, 3);
        LogUtil.info(log, "hello {},{},{}", 1, 2, 3);
    }

}
